// pages/project/project.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {},
    projectList: [],
    page: 1,
    pageSize: 10,
    total: 0,
    isloading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      query: options
    })
    this.getProjectList()
  },

  navigateToProgress(e) {
    const id = e.currentTarget.dataset.id;
    const name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: `/pages/projectProgress/projectProgress?id=${id}&title=${name}`
    });
  },
  navigateToCost(e) {
    const id = e.currentTarget.dataset.id;
    const name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: `/pages/costlist/costlist?id=${id}&title=${name}`
    });
  },
  // 以分页的形式获取商铺列表数据的方法
  getProjectList(cb) {
    this.setData({
      isloading: true
    })
    // 展示 loading 效果
    wx.showLoading({
      title: '数据加载中...'
    })

    wx.request({
      url: `https://applet-base-api-t.itheima.net/categories/1/shops`,
      method: 'GET',
      data: {
        _page: this.data.page,
        _limit: this.data.pageSize
      },
      success: (res) => {
        this.setData({
          projectList: [{
              "createBy": null,
              "createTime": null,
              "updateBy": null,
              "updateTime": "2025-03-30 15:57:03",
              "remark": null,
              "projectId": "d87dc466fa9044218ab35c14b08fcac9",
              "projectName": "汽车零部件",
              "projectType": "EIA",
              "projectTypeList": [
                "EIA"
              ],
              "customerName": "BYD",
              "startDate": "2025-03-03",
              "endDate": "2025-03-05",
              "salesmanType": "经销商",
              "salesman": "黄黄黄",
              "salesmanId": null,
              "amount": "800000",
              "projectBase": "340101",
              "recordTime": "2025-03-30 15:08:22",
              "recorder": 1,
              "amountRecvSum": null,
              "stackholders": null,
              "attachment": "/profile/upload/2025/03/30/环保技术咨询服务合同20250326_20250330150812A001.pdf",
              "status": "1",
              "rateOfProfit": null,
              "projectStages": null
            },
            {
              "createBy": null,
              "createTime": null,
              "updateBy": null,
              "updateTime": "2025-03-24 22:26:39",
              "remark": "完整测试123",
              "projectId": "f3f81dab861c4ad78b44642b78069c70",
              "projectName": "完整的项目周期测试123",
              "projectType": "EIA",
              "projectTypeList": [
                "EIA"
              ],
              "customerName": "甲方乙方123",
              "startDate": "2025-03-25",
              "endDate": "2025-03-28",
              "salesmanType": "经销商",
              "salesman": "黄黄黄",
              "salesmanId": null,
              "amount": "65500",
              "projectBase": "340103",
              "recordTime": "2025-03-24 22:14:39",
              "recorder": 2,
              "amountRecvSum": null,
              "stackholders": null,
              "attachment": "/profile/upload/2025/03/24/硬件参数_20250324221430A001.png",
              "status": "1",
              "rateOfProfit": null,
              "projectStages": null
            },
            {
              "createBy": null,
              "createTime": null,
              "updateBy": null,
              "updateTime": "2025-03-24 21:57:21",
              "remark": "备注123",
              "projectId": "5b97dd7e681e4847bf58d7a6d0d66575",
              "projectName": "测试",
              "projectType": "FSR",
              "projectTypeList": [
                "FSR"
              ],
              "customerName": "甲方123",
              "startDate": "2025-03-23",
              "endDate": "2025-03-29",
              "salesmanType": "销售",
              "salesman": "九华山",
              "salesmanId": null,
              "amount": "35000",
              "projectBase": "340101",
              "recordTime": "2025-03-23 01:05:18",
              "recorder": 1,
              "amountRecvSum": null,
              "stackholders": null,
              "attachment": "/profile/upload/2025/03/23/Default_20250323010414A001.jpg",
              "status": "0",
              "rateOfProfit": null,
              "projectStages": null
            },
            {
              "createBy": null,
              "createTime": null,
              "updateBy": null,
              "updateTime": "2025-03-24 22:25:54",
              "remark": "",
              "projectId": "b8dc25843e3c48a88bd975056744fcaa",
              "projectName": "附件测试",
              "projectType": "FSR",
              "projectTypeList": [
                "FSR"
              ],
              "customerName": "甲方甲方",
              "startDate": "2025-03-03",
              "endDate": "2025-04-10",
              "salesmanType": "销售",
              "salesman": "九华山",
              "salesmanId": null,
              "amount": "65300",
              "projectBase": "340101",
              "recordTime": "2025-03-04 22:35:19",
              "recorder": 1,
              "amountRecvSum": null,
              "stackholders": null,
              "attachment": "/profile/upload/2025/03/04/Default_20250304223516A001.jpg",
              "status": "1",
              "rateOfProfit": null,
              "projectStages": null
            },
            {
              "createBy": null,
              "createTime": null,
              "updateBy": null,
              "updateTime": "2025-03-04 22:46:28",
              "remark": "",
              "projectId": "fd6dcb79b63f444fadcc2330d57981f1",
              "projectName": "附件测试",
              "projectType": "FSR",
              "projectTypeList": [
                "FSR"
              ],
              "customerName": "甲方甲方",
              "startDate": "2025-03-03",
              "endDate": "2025-04-10",
              "salesmanType": "销售",
              "salesman": "九华山",
              "salesmanId": null,
              "amount": "65300",
              "projectBase": "340101",
              "recordTime": "2025-03-04 22:35:19",
              "recorder": 1,
              "amountRecvSum": null,
              "stackholders": null,
              "attachment": "/profile/upload/2025/03/04/Default_20250304223516A001.jpg",
              "status": "0",
              "rateOfProfit": null,
              "projectStages": null
            }
          ],
          total: res.header['X-Total-Count'] - 0
        })
      },
      complete: () => {
        // 隐藏 loading 效果
        wx.hideLoading()
        this.setData({
          isloading: false
        })
        // wx.stopPullDownRefresh()
        cb && cb()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: "项目列表"
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 需要重置关键的数据
    this.setData({
      page: 1,
      projectList: [],
      total: 0
    })
    // 重新发起数据请求
    this.getProjectList(() => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page * this.data.pageSize >= this.data.total) {
      // 证明没有下一页的数据了
      return wx.showToast({
        title: '数据加载完毕！',
        icon: 'none'
      })
    }
    // 判断是否正在加载其他数据
    if (this.data.isloading) return
    // 页码值 +1
    this.setData({
      page: this.data.page + 1
    })

    // 获取下一页数据
    this.getProjectList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})